﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Net;
using TheMovieDB;
using System.Data.SqlClient;

namespace GenerateMovieList
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            lblVersion.Text = version.Substring(0, version.Length - 2);
            lblProduct.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
        }
        private tmdb tmdbcl;
        private images imagescl;
        
        private int movies;
        private string runned = "";

        private void search(string folder)
        {
            tmdbcl = new tmdb();
            imagescl = new images();
            movies = 0;
            string output = "";
            tsslStatus.Text = "Scanning folders...";
            this.Text = "Movie List Generator - Generating List";
            string[] filePaths = Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories);
            foreach (string file in filePaths)
            {
                FileInfo fi = new FileInfo(file);
                string extension = fi.Extension.ToLower();
                if (extension == ".avi" || extension == ".mkv" || extension == ".mp4" || extension == ".movie" || extension == ".wmv" || extension == ".iso" || extension == ".img")
                {                    
                    string localxmlfile = fi.DirectoryName + @"\info.xml";
                    string movie = System.IO.Path.GetFileNameWithoutExtension(file);
                    int filenamelenght = 0;
                    if (System.IO.Path.GetFileNameWithoutExtension(file).Contains("CD1"))
                    {
                        movie = System.IO.Path.GetFileNameWithoutExtension(file).Replace("CD1", "");
                        filenamelenght = movie.Length;
                        runned += movie + " ";
                    }
                    if (System.IO.Path.GetFileNameWithoutExtension(file).Contains("CD2"))
                    {
                        movie = System.IO.Path.GetFileNameWithoutExtension(file).Replace("CD2", "");
                        filenamelenght = movie.Length;
                        runned += movie + " ";
                    }
                    if (System.IO.Path.GetFileNameWithoutExtension(file).Length == filenamelenght)
                    {
                        movie = System.IO.Path.GetFileNameWithoutExtension(file);
                    }
                    tsslStatus.Text = "Movie processing: " + movie;
                    if (File.Exists(localxmlfile))
                    {
                        tmdbcl.uselocalxml(localxmlfile);
                        if (cbLocalinfo.Checked == true)
                        {
                            if (tmdbcl.mdid != string.Empty && tmdbcl.mdname != string.Empty)
                            {
                                tmdbcl.themoviedbmissing(movie, fi.DirectoryName, tmdbcl.mdid, tmdbcl.mdname);
                                tmdbcl.generatexmldetails(localxmlfile, movie, fi.Extension, fi.DirectoryName, "Movie", "Details");
                                tmdbcl.localnfofile(fi.DirectoryName);
                                tmdbcl.shortcutimdb(fi.DirectoryName);
                            }
                            else
                            {
                                MessageBox.Show("Local XML file corrupt for movie: " + movie + ", will be deleted. Please rerun the application for the movie again", "Corrupt local XML file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                File.Delete(localxmlfile);
                            }
                        }
                        if (cbImages.Checked == true)
                        {
                            if (tmdbcl.mdbackdrop != string.Empty && tmdbcl.mdposter != string.Empty)
                            {
                                imagescl.downloadimages(tmdbcl.mdbackdrop, fi.DirectoryName, "backdrop");
                                imagescl.downloadimages(tmdbcl.mdposter, fi.DirectoryName, "poster");
                            }
                            else
                            {
                                if (tmdbcl.mdid != string.Empty && tmdbcl.mdname != string.Empty)
                                {
                                    tmdbcl.themoviedbmissing(movie, fi.DirectoryName, tmdbcl.mdid, tmdbcl.mdname);                                
                                    imagescl.downloadimages(tmdbcl.mdbackdrop, fi.DirectoryName, "backdrop");
                                    imagescl.downloadimages(tmdbcl.mdposter, fi.DirectoryName, "poster");
                                }
                                else
                                {
                                    MessageBox.Show("Local XML file corrupt for movie: " + movie + ", will be deleted. Please rerun the application for the movie again", "Corrupt local XML file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    File.Delete(localxmlfile);
                                }
                            }
                        }
                        if (cbCollection.Checked == true)
                        {
                            if (output == string.Empty)
                            {
                                sfdOutput.ShowDialog();
                                output = sfdOutput.FileName;
                                tmdbcl.generatexmlmovies(output, movie, fi.Extension, fi.DirectoryName, "Movies", "Movie");
                            }
                            else
                            {
                                tmdbcl.generatexmlmovies(output, movie, fi.Extension, fi.DirectoryName, "Movies", "Movie");
                            }     
                        }
                        if (tmdbcl.mdid != string.Empty)
                            movies = movies + 1;
                    }
                    else
                    {
                        tmdbcl.themoviedb(movie, fi.DirectoryName);
                        if (cbLocalinfo.Checked == true)
                        {
                            tmdbcl.generatexmldetails(localxmlfile, movie, fi.Extension, fi.DirectoryName, "Movie", "Details");
                            tmdbcl.localnfofile(fi.DirectoryName);
                            tmdbcl.shortcutimdb(fi.DirectoryName);
                        }
                        if (cbImages.Checked == true)
                        {
                            if (tmdbcl.mdbackdrop != string.Empty && tmdbcl.mdposter != string.Empty)
                            {
                                imagescl.downloadimages(tmdbcl.mdbackdrop, fi.DirectoryName, "backdrop");
                                imagescl.downloadimages(tmdbcl.mdposter, fi.DirectoryName, "poster");
                            }
                            else
                            {
                                if (tmdbcl.mdid != string.Empty)
                                {
                                    tmdbcl.themoviedb(movie, fi.DirectoryName);
                                    imagescl.downloadimages(tmdbcl.mdbackdrop, fi.DirectoryName, "backdrop");
                                    imagescl.downloadimages(tmdbcl.mdposter, fi.DirectoryName, "poster");
                                }
                            }
                        }
                        if (cbCollection.Checked == true)
                        {
                            if (output == string.Empty)
                            {
                                sfdOutput.ShowDialog();
                                output = sfdOutput.FileName;
                                tmdbcl.generatexmlmovies(output, movie, fi.Extension, fi.DirectoryName, "Movies", "Movie");
                            }
                            else
                            {
                                tmdbcl.generatexmlmovies(output, movie, fi.Extension, fi.DirectoryName, "Movies", "Movie");
                            } 
                        }
                        if (tmdbcl.mdid != string.Empty)
                            movies = movies + 1;
                    }
                    this.Text = "Movie List Generator";
                    tsslStatus.Text = "Processing " + movies + " movies done...";
                }
            }
        }             

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (File.Exists("TheMovieDBLib.dll"))
            {
                if (tbFolder.Text != string.Empty)
                    search(tbFolder.Text);
                else
                    MessageBox.Show("No folders selected", "Select a folder", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                var error = MessageBox.Show("Vital DLL file is missing, please reinstall", "Missing DLL file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (error == DialogResult.OK)
                    Close();
            }
        }

        private void btnFolder_Click(object sender, EventArgs e)
        {
            fbdFolder.ShowDialog();
            tbFolder.Text = fbdFolder.SelectedPath;
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            var open = ofdXmlFile.ShowDialog();
            if (open == DialogResult.OK)
            {
                ListXML list = new ListXML(ofdXmlFile.FileName);
                list.ShowDialog();
            }
        }

        private void lblWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://code.google.com/p/movielistgenerator/");
        }

        private void lblBugs_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://code.google.com/p/movielistgenerator/issues/list");
        }
    }
}
